import java.math.BigInteger;
import java.util.Base64;
/*
* 主程序
* */
public class Main {
    
    static BigInteger num0=new BigInteger("0");
    static BigInteger num1=new BigInteger("1");
    public static BigInteger normalPower(BigInteger base,BigInteger power,BigInteger n){
        BigInteger result=new BigInteger("1");
        while(power.compareTo(num0)==1){
            if(power.and(num1).compareTo(num1)==0) result=result.multiply(base).mod(n);
            power=power.shiftRight(1);
            base=base.multiply(base).mod(n);
        }
        return result;
    }
    public static void main(String[] args) {
        /*
        * 设置秘钥位数
        * */
        int bits=128;
        RSA rsa=new RSA(bits);
        String text = "我是个加密数据，你是个憨皮！";
        String signData = "我是签名数据！";
        System.out.println("加密数据："+text);
        System.out.println("签名数据："+signData);
        /*--------------------------------------------加密---------------------------------------------*/
        System.out.println("-------------加密测试-------------");
        /*
        * 加密
        * */
        Ciphertext ciphertext = rsa.encrypt(new Plaintext(text));

        String cstr= ciphertext.toString();
        //发送、接收cstr等操作.
        //......


        /*
         * 解密
         * */
        Plaintext plaintext=rsa.decrypt(new Ciphertext(cstr));

        System.out.println("密文："+cstr);
        System.out.println("明文："+plaintext);

        /*--------------------------------------------签名---------------------------------------------*/
        System.out.println("-------------签名测试-------------");
        /*
         * 签名
         * */
        Ciphertext sign=rsa.signature(new Plaintext(signData));
        String signStr= sign.toString();
        //发送、接收signStr等操作.
        //......

        /*
         * 验证
         * */
        Plaintext signV=rsa.VerifySignature(new Ciphertext(signStr));
        String signD=signV.toString();
        System.out.println("签名密文："+signStr);
        System.out.println("签名解密后："+signD);
        System.out.println("验证结果："+(signData.equals(signD)?"成功！":"失败！"));


        /*--------------------------------------------输入秘钥解密---------------------------------------------*/
        System.out.println("-------------输入秘钥解密-------------");
        RSA rsaTool=new RSA();
        String data="ODA5Njg5MDgxODIyMDYxMDE4OS85MzM2NzQzMjA4NDMwMzY3OTIwLzU1NDk0Nzg5MzQ4OTQ3Mjc1NDIvMjk3NDU5MjkwMzM3MTM4MzE1NC80NjAzNjU0NTY4MDAwOTkyNjYzLzE5MTQ3NDMxNDk5OTk1OTE5NS80ODE3NjY3NTA4Nzc5MjEyOTUvNzgyNDEzMjkzMzk2NjI4NDAxOS85ODcyNjE2NTU3MzQwNDQzMTExLzI3MzA4MzQ4MzMwMzI1MzUxMzUvMzI5OTMzNTQyMzY0ODk3MzA4My80MTIzNzYzMTcyNTgyNjM3NTc4LzM1NTI2OTk1MjEzMjY2MjkzNzEvNTQ5NTY3NjczOTAzMDk0MDg1LzgxNzg5NDU2MDcyODkwMTY4MzM=";
        rsaTool.setPrivateKey(new PrivateKey("MTE5OTg5OTg1NTExOTk5NTE1My8xMDA1OTg0ODY0NDczNDQ0ODAxOQ=="));
//        rsaTool.setPublicKey(new PublicKey("NjU1MzcvMTAwNTk4NDg2NDQ3MzQ0NDgwMTk="));
        String res=rsaTool.decrypt(new Ciphertext(data)).toString();
        System.out.println("解密密文："+data);
        System.out.println("解密结果："+res);
    }
}
